package sctu.edu.java.year2016.team07.project.client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;

/**
 * Created by Huang Suixin on 2016/6/15.
 */
public class Client {
    public Client(String userName) {
        User user = new User();
        user.setName(userName);

        //窗口
        ClientFrame frame = new ClientFrame(userName);
        Container con = frame.getContentPane();
        con.setLayout(new FlowLayout(1, 10, 10));

        JPanel pan = new JPanel();
        pan.setOpaque(false);       //设置透明
        JLabel jl = new JLabel("请输入您的电脑有什么问题：");
        jl.setFont(new Font("dialog", 1, 20));
        JTextField questiontext = new JTextField(20);
        pan.add(jl);
        pan.add(questiontext);
        JButton button = new JButton("查询");
        button.setFont(new Font("dialog", 1, 20));
        button.setContentAreaFilled(false);     //设置按钮透明
        button.setBorderPainted(false);     //取消显示边界
        pan.add(button);

        JPanel pan2 = new JPanel();
        pan2.setOpaque(false);
        JLabel l = new JLabel("解决办法：");
        l.setFont(new Font("dialog", 1, 20));
        JTextArea result = new JTextArea(17, 40);
        result.setEditable(false);      //设置文本框不可编辑
        result.setFont(new Font("dialog", 1, 15));      //设置字体
        result.setForeground(Color.RED);
        result.setOpaque(false);
        JScrollPane sp = new JScrollPane(result);
        sp.setOpaque(false);
        sp.getViewport().setOpaque(false);
        pan2.add(l);
        pan2.add(sp);
        //设置按钮事件
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String answer;
                    if ((answer = user.search(questiontext.getText())).equals("")) {
                        result.setText("");      //清空文本区域
                        result.setText("暂无该问题的解决方案");

                        //未查找到解决方案，弹出窗口，询问是否需要反馈
                        JDialog dialog = new JDialog(frame, "");     //创建对话框
                        Container container = dialog.getContentPane();      //
                        JPanel panel = new JPanel();
                        JLabel label = new JLabel("未查找到您的问题，您是否需要提交反馈?");
                        label.setFont(new Font("dialog", 1, 20));
                        JButton recordButton = new JButton("是");
                        recordButton.setContentAreaFilled(false);
                        recordButton.setBorderPainted(false);
                        recordButton.setFont(new Font("dialog", 1, 20));
                        JButton existButton = new JButton("否");
                        existButton.setContentAreaFilled(false);
                        existButton.setBorderPainted(false);
                        existButton.setFont(new Font("dialog", 1, 20));
                        panel.add(label);
                        panel.add(recordButton);
                        panel.add(existButton);
                        container.add(panel);

                        //为反馈按钮添加事件
                        recordButton.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                GetUserInformationFrame getUserInformationFrame = new GetUserInformationFrame(user, questiontext.getText());
                                getUserInformationFrame.setLocation(frame.getWidth() / 2, frame.getHeight() / 2);        //设置弹窗位置
                                dialog.dispose();

                            }
                        });

                        //为退出按钮添加事件
                        existButton.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                dialog.dispose();       //结束对话框
                            }
                        });

                        //对 对话框的初始化设置
                        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                        dialog.setSize(400, 150);
                        dialog.setLocation(frame.getWidth() / 2, frame.getHeight() / 2);     //设置对话框出现的位置
                        dialog.setResizable(false);
                        dialog.setTitle("是否需要提交反馈");
                        dialog.setVisible(true);
                    } else {
                        result.setText("");     //清空文本区域
                        result.setText(answer);     //文本区域显示问题的解决方案

                    }

                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
            }
        });



        con.add(pan);
        con.add(pan2);
        frame.setVisible(true);
    }
}
